//
// Created by jishen on 2020/12/30.
//
#include "jni-lib.h"
#include <jni.h>
extern "C" JNIEXPORT jint
Java_com_jni_sample_JniNative_JniCAdd(JNIEnv *env, jclass obj, jint a, jint b) {
    return a + b;
}

extern "C" JNIEXPORT jint
Java_com_jni_sample_JniNative_JniCSub(JNIEnv *env, jclass obj, jint a, jint b) {
    return a - b;
}

extern "C" JNIEXPORT jstring JNICALL Java_com_jni_sample_JniNative_nativeGetDeviceId(JNIEnv *env, jclass, jobject mContext){
    //t = (double)getTickCount();

    if(mContext == nullptr){
        return env->NewStringUTF("[+] Error: Context is 0");
    }
    jclass cls_context = env->FindClass("android/content/Context");
    if(cls_context == nullptr){
        return env->NewStringUTF("[+] Error: FindClass <android/content/Context> Error");
    }

    jmethodID getSystemService = (env)->GetMethodID(cls_context, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
    if(getSystemService == nullptr){
        return env->NewStringUTF("[+] Error: GetMethodID failed");
    }

    jfieldID TELEPHONY_SERVICE = (env)->GetStaticFieldID(cls_context, "TELEPHONY_SERVICE", "Ljava/lang/String;");
    if(TELEPHONY_SERVICE == nullptr){
        return env->NewStringUTF("[+] Error: GetStaticFieldID Failed");
    }

    auto telephonystr = (jstring)(env)->GetStaticObjectField(cls_context, TELEPHONY_SERVICE);
    jobject telephonymanager = ((env)->CallObjectMethod(mContext, getSystemService, telephonystr));
    if(telephonymanager == nullptr){
        return env->NewStringUTF("[+] Error: CallObjectMethod failed");
    }

    jclass cls_TelephoneManager = (env)->FindClass( "android/telephony/TelephonyManager");
    if(cls_TelephoneManager == nullptr){
        return env->NewStringUTF("[+] Error: FindClass TelephoneManager failed");
    }

    jmethodID getDeviceId = ((env)->GetMethodID(cls_TelephoneManager, "getDeviceId", "()Ljava/lang/String;"));
    if(getDeviceId == nullptr){
        return env->NewStringUTF("[+] Error: GetMethodID getDeviceID failed");
    }

    jobject DeviceID = (env)->CallObjectMethod(telephonymanager,getDeviceId);

    //macID

    jfieldID WIFI_SERVICE = (env)->GetStaticFieldID(cls_context, "WIFI_SERVICE", "Ljava/lang/String;");
    if(WIFI_SERVICE == nullptr){
        return env->NewStringUTF("[+] Error: GetStaticFieldID Failed");
    }

    auto wifistr = (jstring)(env)->GetStaticObjectField(cls_context, WIFI_SERVICE);
    jobject wifimanager = ((env)->CallObjectMethod(mContext, getSystemService, wifistr));
    if(wifimanager == nullptr){
        return env->NewStringUTF("[+] Error: CallObjectMethod failed");
    }

    jclass cls_WifiManager = (env)->FindClass( "android/net/wifi/WifiManager");
    if(cls_WifiManager == nullptr){
        return env->NewStringUTF("[+] Error: FindClass cls_WifiManager failed");
    }

    jmethodID getWifiId = ((env)->GetMethodID(cls_WifiManager, "getConnectionInfo", "()Landroid/net/wifi/WifiInfo;"));

    jobject wifiInfo = env->CallObjectMethod(wifimanager,getWifiId);

    jclass wifiInfoclass = env->GetObjectClass(wifiInfo);

    jmethodID getMacId = env->GetMethodID(wifiInfoclass,"getMacAddress","()Ljava/lang/String;");

    jobject MacID = env->CallObjectMethod(wifiInfo,getMacId);

//    return (jstring)DeviceID;
    return (jstring)MacID;

}

